package com.user.config;

import io.lettuce.core.ReadFrom;
import org.springframework.boot.autoconfigure.data.redis.LettuceClientConfigurationBuilderCustomizer;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import javax.annotation.Resource;
import java.util.List;

@Configuration
public class RedisConfiguration {

    @Resource
    RedisProperties redisProperties;

    @Bean
    public RedisTemplate<String,Object> redisTemplate(){
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(lettuceConnectionFactory()); // 设置redis连接工厂并且实现读写分离
        SerializerRedis(redisTemplate);
        return redisTemplate;
    }

    @Bean
    public StringRedisTemplate stringRedisTemplate(){
        return new StringRedisTemplate(lettuceConnectionFactory());
    }

    @Bean
    public RedisConnectionFactory lettuceConnectionFactory(){
        return new LettuceConnectionFactory(getRedisSentinelConfiguration(), LettuceClientConfiguration.builder()
                .readFrom(ReadFrom.REPLICA_PREFERRED)
                .build());
    }

    /**
     * 专用于主节点的RedisTemplate 用于执行Lua脚本
     */
    @Bean(name = "MasterRedisTemplate")
    public RedisTemplate<String,Object> MasterRedisTemplate(){
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(masterLettuceConnectionFactory()); // 设置为指定的连接工厂 只运行在Master节点上
        SerializerRedis(redisTemplate);
        return redisTemplate;
    }

    @Bean
    public RedisConnectionFactory masterLettuceConnectionFactory(){
        return new LettuceConnectionFactory(getRedisSentinelConfiguration(), LettuceClientConfiguration.builder()
                .readFrom(ReadFrom.MASTER)
                .build());
    }

    private static void SerializerRedis(RedisTemplate<String, Object> redisTemplate) {
        // 配置Key与hashKey序列化器为StringRedisSerializer
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        // 配置Value与hashValue序列化器为GenericJackson2JsonRedisSerializer
        GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
        redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);
    }


    private RedisSentinelConfiguration getRedisSentinelConfiguration() {
        RedisProperties.Sentinel sentinel = redisProperties.getSentinel();
        RedisSentinelConfiguration config = new RedisSentinelConfiguration()
                .master(sentinel.getMaster());
        for (String node : sentinel.getNodes()) {
            String[] urls = node.split(":");
            config.sentinel(urls[0], Integer.parseInt(urls[1]));
        }
        return config;
    }

}
